package com.weavernorth.updateKaoQin;

import com.weavernorth.util.Logger4jUtil;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.wechat.util.Utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import static com.weavernorth.util.MyCommonUtils.getUseridByCode;
import static com.weavernorth.util.dateUtils.*;

/**
 * @Author: bruseewang
 * @Package: com.weavernorth.updateKaoQin
 * @date: 2021/8/2 16:09
 * @description: 定时任务创建：重新计算销假人员考勤
 */
public class updateKq_xjry_Timmer  extends BaseCronJob {
    public Logger log = Logger4jUtil.getLoggerByName("/Timmer/updateKqTimmer/");
    private String gh;
    private String begindate;
    private String endate;
    public String getGh() {
        return gh;
    }
    public void setGh(String gh) {
        this.gh = gh;
    }
    public String getBegindate() {
        return begindate;
    }
    public void setBegindate(String begindate) {
        this.begindate = begindate;
    }
    public String getEndate() {
        return endate;
    }
    public void setEndate(String endate) {
        this.endate = endate;
    }
    @Override
    public void execute() {
        log.info("刷新销假人员考勤定时任务---开始===>");
        String strBegin="";
        String strEnd="";
        String strGh="";
        BaseBean bb=new BaseBean();
        //获取当前日期
        String nowDate = getYearMonthDay();
        String year=nowDate.split("-")[0];
        String month=nowDate.split("-")[1];
        //获取当前日期天数
        String day= nowDate.split("-")[2];
        if (Integer.parseInt(day)>4){//当前日期在4号之后,取本月
            log.info("当前日期在4号之后时：");
            strBegin=year+"-"+month+"-01";
            log.info("开始日期："+strBegin);
            strEnd=nowDate;
            log.info("结束日期："+strEnd);
        }else{
            strBegin=getPreMonthFirstDate();//获取上个月第一天
            log.info("开始日期："+strBegin);
            strEnd=nowDate;
            log.info("结束日期："+strEnd);
        }
        if(begindate!=null){
            if(isValidDate(begindate,"yyyy-MM-dd")){
                strBegin=begindate;
                log.info("日期参数-开始日期："+strBegin);
            }

        }
        if(endate!=null){
            if(isValidDate(endate,"yyyy-MM-dd")) {
                strEnd = endate;
                log.info("日期参数-结束日期："+strEnd);
            }
        }
        if(gh!=null){
            strGh=gh;
        }
        String url=bb.getPropValue("updateKqjl","url");
        String workflowid=bb.getPropValue("updateKqjl","workflowid");
        //销假：workflowid = 2051;
        flushCancleLeave(workflowid,strGh,strBegin,strEnd,url);
    }
    /***
     * 销假人员考勤刷新
     * @param flowid
     * @param strGh
     * @param strBegin
     * @param strEnd
     */
    private void flushCancleLeave(String flowid, String strGh, String strBegin, String strEnd,String url) {
        log.info("销假人员考勤刷新开始===>");
        RecordSet rs=new RecordSet();
        String strSql="SELECT  m.resourceid userid,n.detail_leavefromDate begindate,n.detail_leavetoDate endate,m.requestid,b.currentnodetype  " +
                "FROM  FORMTABLE_MAIN_113 m,FORMTABLE_MAIN_113_dt1 n,workflow_requestbase b WHERE " +
                "m.id=n.mainid and m.requestid=b.requestid and b.workflowid = "+flowid+"  AND b.currentnodetype = 3  AND to_date(n.detail_leavefromDate, 'yyyy-mm-dd') BETWEEN to_date('"+strBegin+"','yyyy-mm-dd')  AND to_date('"+strEnd+"','yyyy-mm-dd')" +
                " AND to_date(n.detail_leavetoDate, 'yyyy-mm-dd') BETWEEN to_date('"+strBegin+"','yyyy-mm-dd')  AND to_date('"+strEnd+"','yyyy-mm-dd')";
        if(!"".equals(strGh)){
            String strUserid= getUseridByCode(strGh);
            strSql+=" and m.resourceid="+strUserid;
        }
        log.info("执行销假申请人员考勤刷新的sql:"+strSql);
        rs.executeQuery(strSql);
        while (rs.next()){
            String strUserid = Utils.null2String(rs.getString("userid"));
            log.info("人员id:"+strUserid);
            String strRequest= Utils.null2String(rs.getString("requestid"));
            log.info("requestId:"+strRequest);
            String begindate = Utils.null2String(rs.getString("begindate"));
            log.info("开始日期:"+begindate);
            String endate = Utils.null2String(rs.getString("endate"));
            log.info("结束日期:"+endate);
            //调用泛微提供的刷新报表短连接,已休不刷新：isUpgrade=true
            String strUrl=url+"?requestid="+strRequest+"&workflowid="+flowid+"&isUpgrade=true";
            log.info("刷新销假人员考勤的链接："+strUrl);
            String strResult= sendGet(strUrl,log);
            log.info("销假申请requestId="+strRequest+";的处理结果："+strResult);

        }
        log.info("刷新销假人员考勤结束===>");
    }

    //发送GET请求
    public static String sendGet(String url, Logger log) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlName = url;
            URL realUrl = new URL(urlName);
            URLConnection conn = realUrl.openConnection();// 打开和URL之间的连接
            conn.setRequestProperty("accept", "*/*");// 设置通用的请求属性
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            conn.setConnectTimeout(4000);
            conn.connect();// 建立实际的连接
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));// 定义BufferedReader输入流来读取URL的响应
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            log.info("发送GET请求出现异常！" + e);
        } finally {// 使用finally块来关闭输入流
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                log.info("关闭流异常");
            }
        }
        return result;
    }
}
